package com.example.elmmerchants.Shop_Activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Activity_Center_Page extends Fragment {

    View root;
    LinearLayout check_all_activity, full_reduction_button, add_my_activity_view, reduce_the_delivery_fee_button, discount_product_button, coupon_button;
    TextView how_many_is_ongoing, how_many_is_quick_overdue;

    String email, shop_name;
    AlertDialog dialog;
    boolean is_loading_data = false;
    int full_reduction_activity_state = 0, reduce_the_delivery_fee_state = 0, discount_product_state = 0, collect_points_to_return_coupon_state = 0, return_coupon_for_placing_an_order_state = 0, new_custom_receive_a_discount_state = 0;
    boolean is_open_full_reduction_activity = false, is_open_reduce_the_delivery_fee = false, is_open_discount_product = false, is_open_collect_and_have_gifts_page = false, is_open_collect_points_to_return_coupon = false, is_open_return_coupon_for_placing_an_order = false, is_open_new_custom_receive_a_discount = false;
    List<Double> full_reduction_list = new ArrayList<>();
    double reduce_the_delivery_fee_reduce_how_many_money = 0;
    int get_how_many_is_quick_overdue = 0, get_how_many_is_ongoing = 0;
    List<String> discount_product_name = new ArrayList<>();
    List<List<String>> discount_product_data = new ArrayList<>();
    int set_how_much_can_use_discount_in_coupon_page = 0, set_how_much_is_the_discount_in_coupon_page = 0;
    int set_how_many_order_in_collect_points = 0, set_how_much_is_the_discount_in_collect_points = 0, set_how_many_day_in_collect_points = 0;
    int set_custom_should_pay_how_many_money_in_return_coupon = 0, set_how_much_can_use_discount_in_return_coupon = 0, set_how_much_is_the_discount_in_return_coupon = 0;
    int new_custom_receive_a_discount_reduce_how_many_money = 0;

    public Activity_Center_Page(String email, String shop_name) {
        this.email = email;
        this.shop_name = shop_name;
    }

    private class Data_Thread extends Thread {
        @Override
        public void run() {
            is_loading_data = false;
            is_open_full_reduction_activity = false;
            is_open_reduce_the_delivery_fee = false;
            is_open_discount_product = false;
            is_open_collect_and_have_gifts_page = false;
            is_open_collect_points_to_return_coupon = false;
            is_open_return_coupon_for_placing_an_order = false;
            is_open_new_custom_receive_a_discount = false;
            get_how_many_is_quick_overdue = 0;
            get_how_many_is_ongoing = 0;
            full_reduction_list.clear();
            discount_product_name.clear();
            discount_product_data.clear();
            set_how_much_can_use_discount_in_coupon_page = 0;
            set_how_much_is_the_discount_in_coupon_page = 0;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant_shop_full_reduction_activity WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set.getString("activity_end_day"));
                        Date today = df.parse(df.format(new Date()));
                        if (date.getTime() + 86400000 < today.getTime()) {
                            full_reduction_activity_state = 2;
                        }
                        else if (date.getTime() == today.getTime()) {
                            full_reduction_activity_state = 3;
                            get_how_many_is_quick_overdue += 1;
                        }
                        else {
                            full_reduction_activity_state = 1;
                            get_how_many_is_ongoing += 1;
                        }
                        for (int i = 1; i <= 5; i++) {
                            if (set.getDouble("full_how_dollars_" + i) == -1) {
                                break;
                            }
                            full_reduction_list.add(set.getDouble("full_how_dollars_" + i));
                            full_reduction_list.add(set.getDouble("subtract_how_dollars_" + i));
                        }
                        is_open_full_reduction_activity = true;
                    }

                    String sql2 = "SELECT * FROM merchant_reduce_the_delivery_fee WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql2);
                    preparedStatement.setString(1, email);
                    ResultSet set2 = preparedStatement.executeQuery();
                    while (set2.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set2.getString("activity_end_day"));
                        Date today = df.parse(df.format(new Date()));
                        if (date.getTime() + 86400000 < today.getTime()) {
                            reduce_the_delivery_fee_state = 2;
                        }
                        else if (date.getTime() == today.getTime()) {
                            reduce_the_delivery_fee_state = 3;
                            get_how_many_is_quick_overdue += 1;
                        }
                        else {
                            reduce_the_delivery_fee_state = 1;
                            get_how_many_is_ongoing += 1;
                        }
                        reduce_the_delivery_fee_reduce_how_many_money = set2.getDouble("reduce_how_many_money");
                        is_open_reduce_the_delivery_fee = true;
                    }

                    String sql3 = "SELECT * FROM merchant_discount_product_page WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql3);
                    preparedStatement.setString(1, email);
                    ResultSet set3 = preparedStatement.executeQuery();
                    while (set3.next()) {
                        List<String> temp = new ArrayList<>();
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set3.getString("activity_end_day"));
                        Date today = df.parse(df.format(new Date()));
                        if (date.getTime() + 86400000 < today.getTime()) {
                            discount_product_state = 2;
                        }
                        else if (date.getTime() == today.getTime()) {
                            discount_product_state = 3;
                            get_how_many_is_quick_overdue += 1;
                        }
                        else {
                            discount_product_state = 1;
                            get_how_many_is_ongoing += 1;
                        }
                        String temp_name = "";
                        for (int i = 0; i < set3.getString("all_product_name").length(); i++) {
                            if (set3.getString("all_product_name").charAt(i) == ',') {
                                discount_product_name.add(temp_name);
                                temp_name = "";
                            }
                            else {
                                temp_name += set3.getString("all_product_name").charAt(i);
                            }
                        }
                        String temp_data = "";
                        for (int i = 0; i < set3.getString("all_product_data").length(); i++) {
                            if (temp_data.equals("所有折扣数据已添加")) {
                                discount_product_data.add(temp);
                                temp_data = "";
                                temp = new ArrayList<>();
                            }
                            else if (set3.getString("all_product_data").charAt(i) == ',') {
                                temp.add(temp_data);
                                temp_data = "";
                            }
                            else {
                                temp_data += set3.getString("all_product_data").charAt(i);
                            }
                        }
                        is_open_discount_product = true;
                    }

                    String sql4 = "SELECT * FROM merchant_coupon_collect_and_get_give WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql4);
                    preparedStatement.setString(1, email);
                    ResultSet set4 = preparedStatement.executeQuery();
                    while (set4.next()) {
                        get_how_many_is_ongoing += 1;
                        set_how_much_can_use_discount_in_coupon_page = set4.getInt("set_how_much_can_use_discount");
                        set_how_much_is_the_discount_in_coupon_page = set4.getInt("set_how_much_is_the_discount");
                        is_open_collect_and_have_gifts_page = true;
                    }

                    String sql5 = "SELECT * FROM merchant_coupon_collect_points_to_return_coupon WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql5);
                    preparedStatement.setString(1, email);
                    ResultSet set5 = preparedStatement.executeQuery();
                    while (set5.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set5.getString("activity_end_day"));
                        Date today = df.parse(df.format(new Date()));
                        if (date.getTime() + 86400000 < today.getTime()) {
                            collect_points_to_return_coupon_state = 2;
                        }
                        else if (date.getTime() == today.getTime()) {
                            collect_points_to_return_coupon_state = 3;
                            get_how_many_is_quick_overdue += 1;
                        }
                        else {
                            collect_points_to_return_coupon_state = 1;
                            get_how_many_is_ongoing += 1;
                        }
                        set_how_many_day_in_collect_points = set5.getInt("set_how_many_day");
                        set_how_many_order_in_collect_points = set5.getInt("set_how_many_order");
                        set_how_much_is_the_discount_in_collect_points = set5.getInt("set_how_much_is_the_discount");
                        is_open_collect_points_to_return_coupon = true;
                    }

                    String sql6 = "SELECT * FROM merchant_coupon_return_coupon_for_placing_an_order WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql6);
                    preparedStatement.setString(1, email);
                    ResultSet set6 = preparedStatement.executeQuery();
                    while (set6.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set6.getString("activity_end_day"));
                        Date today = df.parse(df.format(new Date()));
                        if (date.getTime() + 86400000 < today.getTime()) {
                            return_coupon_for_placing_an_order_state = 2;
                        }
                        else if (date.getTime() == today.getTime()) {
                            return_coupon_for_placing_an_order_state = 3;
                            get_how_many_is_quick_overdue += 1;
                        }
                        else {
                            return_coupon_for_placing_an_order_state = 1;
                            get_how_many_is_ongoing += 1;
                        }
                        set_custom_should_pay_how_many_money_in_return_coupon = set6.getInt("set_custom_should_pay_how_many_money");
                        set_how_much_can_use_discount_in_return_coupon = set6.getInt("set_how_much_can_use_discount");
                        set_how_much_is_the_discount_in_return_coupon = set6.getInt("set_how_much_is_the_discount");
                        is_open_return_coupon_for_placing_an_order = true;
                    }

                    String sql7 = "SELECT * FROM merchant_new_custom_receive_a_discount WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql7);
                    preparedStatement.setString(1, email);
                    ResultSet set7 = preparedStatement.executeQuery();
                    while (set7.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set7.getString("activity_end_day"));
                        Date today = df.parse(df.format(new Date()));
                        if (date.getTime() + 86400000 < today.getTime()) {
                            new_custom_receive_a_discount_state = 2;
                        }
                        else if (date.getTime() == today.getTime()) {
                            new_custom_receive_a_discount_state = 3;
                            get_how_many_is_quick_overdue += 1;
                        }
                        else {
                            new_custom_receive_a_discount_state = 1;
                            get_how_many_is_ongoing += 1;
                        }
                        new_custom_receive_a_discount_reduce_how_many_money = set7.getInt("reduce_how_many_money");
                        is_open_new_custom_receive_a_discount = true;
                    }
                    is_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    how_many_is_quick_overdue.setText(String.valueOf(get_how_many_is_quick_overdue));
                    how_many_is_ongoing.setText(String.valueOf(get_how_many_is_ongoing));
                    if (is_open_full_reduction_activity && full_reduction_activity_state != 2) {
                        View view = LayoutInflater.from(root.getContext()).inflate(R.layout.my_activity_in_center_page, null);
                        TextView activity_name = view.findViewById(R.id.activity_name);
                        activity_name.setText("满减活动");
                        LinearLayout activity_view = view.findViewById(R.id.activity_view);
                        activity_view.removeAllViews();
                        for (int i = 0; i < full_reduction_list.size(); i += 2) {
                            View view1 = LayoutInflater.from(root.getContext()).inflate(R.layout.has_which_activity_in_center_page, null);
                            TextView which_activity = view1.findViewById(R.id.which_activity);
                            which_activity.setText("满" + full_reduction_list.get(i) + "减" + full_reduction_list.get(i + 1));

                            activity_view.addView(view1);
                        }

                        add_my_activity_view.addView(view);
                    }

                    if (is_open_reduce_the_delivery_fee && reduce_the_delivery_fee_state != 2) {
                        View view = LayoutInflater.from(root.getContext()).inflate(R.layout.my_activity_in_center_page, null);
                        TextView activity_name = view.findViewById(R.id.activity_name);
                        activity_name.setText("减配送费活动");
                        LinearLayout activity_view = view.findViewById(R.id.activity_view);
                        activity_view.removeAllViews();
                        View view1 = LayoutInflater.from(root.getContext()).inflate(R.layout.has_which_activity_in_center_page, null);
                        TextView which_activity = view1.findViewById(R.id.which_activity);
                        which_activity.setText("配送费立减" + reduce_the_delivery_fee_reduce_how_many_money + "元");

                        activity_view.addView(view1);

                        add_my_activity_view.addView(view);
                    }

                    if (is_open_discount_product && discount_product_state != 2) {
                        View view = LayoutInflater.from(root.getContext()).inflate(R.layout.my_activity_in_center_page, null);
                        TextView activity_name = view.findViewById(R.id.activity_name);
                        activity_name.setText("折扣商品活动");
                        LinearLayout activity_view = view.findViewById(R.id.activity_view);
                        activity_view.removeAllViews();
                        for (int i = 0; i < discount_product_name.size(); i++) {
                            View view1 = LayoutInflater.from(root.getContext()).inflate(R.layout.has_which_activity_in_center_page, null);
                            TextView which_activity = view1.findViewById(R.id.which_activity);
                            which_activity.setText("商品: " + discount_product_name.get(i) + "\n折后价: " + discount_product_data.get(i).get(0));
                            activity_view.addView(view1);
                        }

                        add_my_activity_view.addView(view);
                    }

                    if (is_open_collect_and_have_gifts_page) {
                        View view = LayoutInflater.from(root.getContext()).inflate(R.layout.my_activity_in_center_page, null);
                        TextView activity_name = view.findViewById(R.id.activity_name);
                        activity_name.setText("收藏有礼活动");
                        LinearLayout activity_view = view.findViewById(R.id.activity_view);
                        activity_view.removeAllViews();
                        View view1 = LayoutInflater.from(root.getContext()).inflate(R.layout.has_which_activity_in_center_page, null);
                        TextView which_activity = view1.findViewById(R.id.which_activity);
                        which_activity.setText("新用户收藏满¥" + set_how_much_can_use_discount_in_coupon_page + "减" + set_how_much_is_the_discount_in_coupon_page + "元");

                        activity_view.addView(view1);

                        add_my_activity_view.addView(view);
                    }

                    if (is_open_collect_points_to_return_coupon && collect_points_to_return_coupon_state != 2) {
                        View view = LayoutInflater.from(root.getContext()).inflate(R.layout.my_activity_in_center_page, null);
                        TextView activity_name = view.findViewById(R.id.activity_name);
                        activity_name.setText("集点返券活动");
                        LinearLayout activity_view = view.findViewById(R.id.activity_view);
                        activity_view.removeAllViews();
                        View view1 = LayoutInflater.from(root.getContext()).inflate(R.layout.has_which_activity_in_center_page, null);
                        TextView which_activity = view1.findViewById(R.id.which_activity);
                        which_activity.setText(set_how_many_day_in_collect_points + "日内集满" + set_how_many_order_in_collect_points + "单即可返券" + set_how_much_is_the_discount_in_collect_points + "元");

                        activity_view.addView(view1);

                        add_my_activity_view.addView(view);
                    }

                    if (is_open_return_coupon_for_placing_an_order && return_coupon_for_placing_an_order_state != 2) {
                        View view = LayoutInflater.from(root.getContext()).inflate(R.layout.my_activity_in_center_page, null);
                        TextView activity_name = view.findViewById(R.id.activity_name);
                        activity_name.setText("下单返券活动");
                        LinearLayout activity_view = view.findViewById(R.id.activity_view);
                        activity_view.removeAllViews();
                        View view1 = LayoutInflater.from(root.getContext()).inflate(R.layout.has_which_activity_in_center_page, null);
                        TextView which_activity = view1.findViewById(R.id.which_activity);
                        which_activity.setText("订单实付金额满" + set_custom_should_pay_how_many_money_in_return_coupon + "元即可返券" + set_how_much_can_use_discount_in_return_coupon + "减" + set_how_much_is_the_discount_in_coupon_page + "元");

                        activity_view.addView(view1);

                        add_my_activity_view.addView(view);
                    }

                    if (is_open_new_custom_receive_a_discount && new_custom_receive_a_discount_state != 2) {
                        View view = LayoutInflater.from(root.getContext()).inflate(R.layout.my_activity_in_center_page, null);
                        TextView activity_name = view.findViewById(R.id.activity_name);
                        activity_name.setText("门店新客立减活动");
                        LinearLayout activity_view = view.findViewById(R.id.activity_view);
                        activity_view.removeAllViews();
                        View view1 = LayoutInflater.from(root.getContext()).inflate(R.layout.has_which_activity_in_center_page, null);
                        TextView which_activity = view1.findViewById(R.id.which_activity);
                        which_activity.setText("门店新客立减" + new_custom_receive_a_discount_reduce_how_many_money + "元");

                        activity_view.addView(view1);

                        add_my_activity_view.addView(view);
                    }
                }
            });
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_activity__center__page, container, false);

        check_all_activity = root.findViewById(R.id.check_all_activity);
        full_reduction_button = root.findViewById(R.id.full_reduction_button);
        add_my_activity_view = root.findViewById(R.id.add_my_activity_view);
        how_many_is_ongoing = root.findViewById(R.id.how_many_is_ongoing);
        how_many_is_quick_overdue = root.findViewById(R.id.how_many_is_quick_overdue);
        reduce_the_delivery_fee_button = root.findViewById(R.id.reduce_the_delivery_fee_button);
        discount_product_button = root.findViewById(R.id.discount_product_button);
        coupon_button = root.findViewById(R.id.coupon_button);

        set_dialog();
        Data_Thread data_thread = new Data_Thread();
        data_thread.start();

        check_all_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(root.getContext(), Check_All_Activity_Page.class);
                intent.putExtra("email", email);
                intent.putExtra("shop_name", shop_name);
                startActivityForResult(intent, 1);
            }
        });

        full_reduction_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if (full_reduction_activity_state != 1) {
                   Intent intent = new Intent(root.getContext(), Full_Reduction_Page.class);
                   intent.putExtra("email", email);
                   startActivityForResult(intent, 1);
               }
               else {
                   Toast.makeText(root.getContext(), "滿減活动已创建", Toast.LENGTH_SHORT).show();
               }
            }
        });

        reduce_the_delivery_fee_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (reduce_the_delivery_fee_state != 1) {
                    Intent intent = new Intent(root.getContext(), Reduce_The_Delivery_Fee_Page.class);
                    intent.putExtra("email", email);
                    startActivityForResult(intent, 1);
                }
                else {
                    Toast.makeText(root.getContext(), "减配送费活动已创建", Toast.LENGTH_SHORT).show();
                }
            }
        });

        discount_product_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (discount_product_state != 1) {
                    Intent intent = new Intent(root.getContext(), Discount_Product_Page.class);
                    intent.putExtra("email", email);
                    startActivityForResult(intent, 1);
                }
                else {
                    Toast.makeText(root.getContext(), "折扣商品活动已创建", Toast.LENGTH_SHORT).show();
                }
            }
        });

        coupon_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog1 = new Dialog(view.getContext());
                dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog1.setContentView(R.layout.choose_coupon_type_view);

                ImageView close = dialog1.findViewById(R.id.close);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog1.dismiss();
                    }
                });

                LinearLayout collect_and_have_gifts = dialog1.findViewById(R.id.collect_and_have_gifts);
                collect_and_have_gifts.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (is_open_collect_and_have_gifts_page) {
                            Toast.makeText(root.getContext(), "收藏有礼活动已创建", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intent = new Intent(dialog1.getContext(), Collect_And_Have_Gifts_Page.class);
                            intent.putExtra("email", email);
                            intent.putExtra("shop_name", shop_name);
                            startActivityForResult(intent, 1);
                        }
                    }
                });

                LinearLayout collect_points_to_return_coupon = dialog1.findViewById(R.id.collect_points_to_return_coupon);
                collect_points_to_return_coupon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (is_open_collect_points_to_return_coupon) {
                            Toast.makeText(root.getContext(), "集点返券活动已创建", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intent = new Intent(dialog1.getContext(), Collect_Points_To_Return_Coupon_Page.class);
                            intent.putExtra("email", email);
                            intent.putExtra("shop_name", shop_name);
                            startActivityForResult(intent, 1);
                        }
                    }
                });

                LinearLayout return_coupon_for_placing_an_order = dialog1.findViewById(R.id.return_coupon_for_placing_an_order);
                return_coupon_for_placing_an_order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (is_open_return_coupon_for_placing_an_order) {
                            Toast.makeText(root.getContext(), "下单返券活动已创建", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intent = new Intent(dialog1.getContext(), Return_Coupon_For_Placing_An_Order_Page.class);
                            intent.putExtra("email", email);
                            intent.putExtra("shop_name", shop_name);
                            startActivityForResult(intent, 1);
                        }
                    }
                });

                dialog1.show();
                dialog1.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog1.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        return root;
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(root.getContext());
        LayoutInflater inflater1 = LayoutInflater.from(root.getContext());
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 1: {
                add_my_activity_view.removeAllViews();
                set_dialog();
                Data_Thread data_thread = new Data_Thread();
                data_thread.start();
            }
        }
    }
}